Completed
Push — master ( 75bd7c...e4f63a )
by Andres
27s
created

dark.js ➔ dark   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 70

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
c 2
b 0
f 0
nc 1
dl 0
loc 70
rs 9.1724
nop 6

5 Functions

Rating   Name   Duplication   Size   Complexity  
A dark.js ➔ ... ➔ ct.darkProduction 0 12 3
A dark.js ➔ ... ➔ ct.visibleDarkUpgrades 0 3 1
A dark.js ➔ ... ➔ isDarkUpgradeVisible 0 3 1
C dark.js ➔ ... ➔ ct.darkPrestige 0 28 7
A dark.js ➔ ... ➔ ct.buyDarkUpgrade 0 11 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
/**
2
 dark
3
 Component that handles dark matter and second prestige logic.
4
 It handles dark matter production and dark upgrades.
5
 A second prestige resets the progress of all elements with exotic
6
 matter and produces dark matter.
7
8
 @namespace Components
9
 */
10
'use strict';
11
12
angular.module('game').component('dark', {
13
  templateUrl: 'views/dark.html',
14
  controller: ['state', 'format', 'visibility', 'upgrade', 'data', 'util', dark],
15
  controllerAs: 'ct'
16
});
17
18
function dark(state, format, visibility, upgrade, data, util) {
19
  let ct = this;
20
  ct.state = state;
21
  ct.data = data;
22
  ct.util = util;
23
  ct.format = format;
24
25
  ct.darkProduction = function() {
26
    let production = 0;
27
    for (let element in data.elements) {
28
      let exotic = data.elements[element].exotic;
29
      if (!state.player.resources[exotic].unlocked) {
30
        continue;
31
      }
32
      production += Math.floor(Math.max(0, Math.log(state.player.resources[exotic].number)));
33
    }
34
35
    return production;
36
  };
37
38
  ct.darkPrestige = function() {
39
    let resources = state.player.resources;
40
    let production = ct.darkProduction();
41
42
    resources.dark_matter.number += production;
43
    resources.dark_matter.unlocked = true;
44
45
    for(let key in data.elements){
46
      let element = data.elements[key];
47
      state.player.resources[element.exotic].number = 0;
48
      if(!state.player.exotic_upgrades[key]){
49
        continue;
50
      }
51
      for(let up in data.exotic_upgrades){
52
        state.player.exotic_upgrades[key][up] = false;
53
      }
54
    }
55
    for(let slot of state.player.element_slots){
56
      if(!slot){
57
        continue;
58
      }
59
      upgrade.resetElement(state.player, slot.element);
60
    }
61
62
    for(let i in state.player.element_slots){
63
      state.player.element_slots[i] = null;
64
    }
65
  };
66
67
  ct.buyDarkUpgrade = function(name) {
68
    let upgrades = state.player.dark_upgrades;
69
    let price = data.dark_upgrades[name].price;
70
    let currency = 'dark_matter';
71
    upgrade.buyUpgrade(state.player,
72
      upgrades,
73
      data.dark_upgrades[name],
74
      name,
75
      price,
76
      currency);
77
  };
78
79
  ct.visibleDarkUpgrades = function() {
80
    return visibility.visible(data.dark_upgrades, isDarkUpgradeVisible, 0);
81
  };
82
83
  // here we receive not the name of the element, but the index in the element_slots
84
  function isDarkUpgradeVisible(name, index) {
85
    return visibility.isUpgradeVisible(name, index, data.dark_upgrades[name]);
86
  }
87
}
88